/**
 * 
 */
package com.buss.core.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dao.DataModelDao;
import com.buss.core.model.DataModel;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;

/**
 * @author 高小勇
 * 创建时间:2011-5-20
 * wcms2.0
 */
@Repository
public class DataModelDaoImpl implements DataModelDao {

	@Autowired
	private BaseDao<DataModel> baseDao;
	
	public void saveDataModel(DataModel dataModel) {
		baseDao.save(dataModel);

	}

	public void editDataModel(DataModel dataModel) {
		baseDao.edit(dataModel);

	}


	/**
	 * 更新列表模型排序序号
	 * @param ids
	 */
	public void updateListSort(Long[] ids){
		String sql = "update DataModel dataModel set dataModel.listSortNum = :sortNum where dataModel.id = :id ";
		for(int i = 0;i < ids.length;i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sortNum", i);
			map.put("id", ids[i]);
			baseDao.excuteHsql(sql, map);
		}
	}
	
	/**
	 * 更正编辑页面排序序号
	 * @param ids
	 */
	public void updateEditSort(Long[] ids){
		String sql = "update DataModel dataModel set dataModel.editSortNum = :sortNum where dataModel.id = :id ";
		for(int i = 0;i < ids.length;i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sortNum", i);
			map.put("id", ids[i]);
			baseDao.excuteHsql(sql, map);
		}
	}
	
	public void deleteDataModel(Long[] ids) {
		String hql = "delete from DataModel dataModel where dataModel.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		baseDao.excuteHsql(hql, map);

	}

	
	public DataModel loadDataModel(Long id) {		
		return baseDao.load(id, DataModel.class);
	}

	
	public ResultList<DataModel> queryDataModel(Long menuId, DataModel dataModel) {
		String hql = "select dataModel from DataModel dataModel where dataModel.menu.id = :menuId ";
		String orderSql = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", menuId);
		if(dataModel != null){
			if(StringUtil.isNotEmpty(dataModel.getPropertyName())){
				hql += " and dataModel.propertyName like :propertyName ";
				map.put("propertyName", "%" + dataModel.getPropertyName() + "%");
			}
			if(StringUtil.isNotEmpty(dataModel.getDefTitle())){
				hql += " and dataModel.defTitle like :defTitle ";
				map.put("defTitle", "%" +dataModel.getDefTitle()+ "%");
			}
			if(StringUtil.isNotEmpty(dataModel.getTitle())){
				hql += " and dataModel.title like :title ";
				map.put("title", dataModel.getTitle());
			}
			if(dataModel.getListIsShow() != null){
				hql += " and dataModel.listIsShow = :listIsShow ";
				map.put("listIsShow", dataModel.getListIsShow());
				orderSql = " order by dataModel.listSortNum asc";
			}
			if(dataModel.getEditIsShow() != null){
				hql += " and dataModel.editIsShow = :editIsShow ";
				map.put("editIsShow", dataModel.getEditIsShow());
				orderSql = " order by dataModel.editSortNum asc ";
			}
			if(dataModel.getIsSearch() != null){
				hql += " and dataModel.isSearch = :isSearch ";
				map.put("isSearch", dataModel.getIsSearch());
			}
		}
		hql += orderSql;
		return baseDao.queryHsql(hql, map, null);
	}

}
